package com.mf.morefun.ui.person_detail.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.mf.morefun.R;
import com.mf.morefun.base.BaseBusFragment;
import com.mf.morefun.bean.ClassBean;
import com.mf.morefun.constant.Configuration;
import com.mf.morefun.listener.MyRequestListener;
import com.mf.morefun.requestbean.RequestClassBean;
import com.mf.morefun.ui.myclass.fragment.HomeWorkFragment;
import com.mf.morefun.ui.person_detail.PersonDetailActivity;
import com.mf.morefun.util.TransformationUtils;
import com.mf.morefun.widget.NetErrorPop;
import com.mf.morefun.widget.TransformationPersonUtils;
import com.zhcx.zhcxlibrary.utils.DateUtils;
import com.zhcx.zhcxlibrary.utils.DeviceUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class PersonInfoFragment extends BaseBusFragment {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.rl_head)
    RelativeLayout rlHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_password)
    TextView tvPassword;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_birth)
    TextView tvBirth;
    @BindView(R.id.tv_class)
    TextView tvClass;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    private String head, name, phone, password, birth, city, userId, enableEnd, address, userType;

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_person;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        Bundle args = getArguments();
        if (null != args) {
            head = args.getString("head");
            name = args.getString("name");
            phone = args.getString("phone");
            password = args.getString("password");
            birth = args.getString("birth");
            city = args.getString("city");
            userId = args.getString("userId");
            enableEnd = args.getString("enableEnd");
            address = args.getString("address");
            userType = args.getString("userType");
        } else {
            head = "";
            name = "";
            phone = "";
            password = "";
            birth = "";
            city = "";
            userId = "";
            enableEnd = "";
            address = "";
            userType = "";
        }
        setPersonData(head, name, phone, password, birth, city, userId, enableEnd, address);
    }
    public static PersonInfoFragment newInstance(String head, String name, String phone, String password, String birth, String city, String userId, String enableEnd, String address, String userType) {
        PersonInfoFragment newFragment = new PersonInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putString("head", head);
        bundle.putString("name", name);
        bundle.putString("phone", phone);
        bundle.putString("password", password);
        bundle.putString("birth", birth);
        bundle.putString("city", city);
        bundle.putString("userId", userId);
        bundle.putString("enableEnd", enableEnd);
        bundle.putString("address", address);
        bundle.putString("userType", userType);
        newFragment.setArguments(bundle);
        return newFragment;
    }
    private void showCicleImg(String url) {
        CircleCrop circleCrop = new CircleCrop();
        RequestOptions options = RequestOptions.bitmapTransform(circleCrop);
        Glide.with(getActivity()).asBitmap().load(url).listener(new MyRequestListener(ivHead, getActivity(), DeviceUtils.dip2px(getActivity(), 60), DeviceUtils.dip2px(getActivity(), 60))).apply(options).into(new TransformationUtils(ivHead, getActivity(), DeviceUtils.dip2px(getActivity(), 60), DeviceUtils.dip2px(getActivity(), 60)));
    }
    public void setPersonData(String head, String name, String phone, String password, String birth, String city, String userId, String enableEnd, String address){
        showCicleImg(head);
        tvName.setText(name);
        tvPhone.setText(phone);
        tvPassword.setText(password);
        tvBirth.setText(birth);
        tvCity.setText(city);
        try {
            if (StringUtils.isEmpty(enableEnd)) {
                tvTime.setText("");
            } else {
                if (enableEnd.length() == 10) {
                    tvTime.setText(enableEnd);
                } else {
                    long enableTime = DateUtils.DEFAULT_DATE_FORMAT.parse(enableEnd).getTime();
                    tvTime.setText(DateUtils.DATE_YEAR_MONTH_DAY.format(new Date(enableTime)));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            tvTime.setText("");
        }
        tvAddress.setText(address);
        if (!StringUtils.isEmpty(userType)){
            if (userType.equals("3")){
                tvClass.setVisibility(View.INVISIBLE);
            }else {
                initClass(userId);
            }
        }else {
            initClass(userId);
        }
    }

    private void initClass(String userId) {
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.QUERYCLASS);
        RequestClassBean requestClassBean = new RequestClassBean();
        RequestClassBean.UserBean userBean = new RequestClassBean.UserBean();
        userBean.setId(userId);
        requestClassBean.setUser(userBean);
        params.setBodyContent(JSON.toJSONString(requestClassBean));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!StringUtils.isEmpty(result)) {
                    ClassBean classBean = JSON.parseObject(result, ClassBean.class);
                    if (classBean.getErrorCode() == 0) {
                        if (null != classBean.getResult() && classBean.getResult().size() > 0){
                            String className = "";
                            if (null != classBean.getResult() && classBean.getResult().size() > 0){
                                className = className + ";" + classBean.getResult().get(0).getClassBelong().getName();
                            }
                            tvClass.setText(className.substring(1));
                        }else {
                            tvClass.setText("无");
                        }
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mHandler.sendEmptyMessage(101);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 101:
                    NetErrorPop errorPop = new NetErrorPop(getActivity());
                    errorPop.showPop("");
                    break;
            }
        }
    };

    @Override
    protected void initData() {

    }
}
